/*
 * Copyright (c) 2008 Sergey Rudchenko <sergey.rudchenko@gmail.com>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "sky.h"
#include "game.h"
#include "sprite.h"
#include "image_pool.h"

#include <stdlib.h>
#include <math.h>

#define SKY_STAR_DENSITY 0.0003
#define STAR_SPEED       0.8

struct star
{
    struct sprite* sprite;
    double speed;
};

static double random_double(double max)
{
    double result = (double)(max) * (rand() / (double)(RAND_MAX));
    return (int)(result);
}

static void generate_stars(struct sky* sky, struct game* ctx)
{
    int stars_count = (int)(ctx->area.h * ctx->area.w * SKY_STAR_DENSITY);

    for (; stars_count > 0; --stars_count)
    {
        struct star* obj = malloc(sizeof(struct star));
        obj->sprite = new_sprite(get_image(IMAGE_STAR));
        sprite_move(obj->sprite, random_double(ctx->area.w), random_double(ctx->area.h));
        sprite_sync(obj->sprite);
        obj->speed = STAR_SPEED;
        sky->stars = g_slist_append(sky->stars, obj);
    }
}

struct sky* new_sky(struct game* ctx)
{
    struct sky* obj = malloc(sizeof(struct sky));
    obj->stars = NULL;

    SDL_Surface* surface;
    surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
                                   ctx->area.w, ctx->area.h,
                                   0, 0, 0, 0, 0);

    obj->image = SDL_DisplayFormat(surface);
    SDL_BlitSurface(obj->image, NULL, ctx->screen, &ctx->area);
    SDL_FreeSurface(surface);

    Uint32 black_color = SDL_MapRGB(obj->image->format, 0, 0, 0);
    SDL_FillRect(obj->image, NULL, black_color);
    generate_stars(obj, ctx);
    return obj;
}

void free_sky(struct sky* sky)
{
    GSList* entry = sky->stars;
    while (entry)
    {
        struct star* star = entry->data;
        free(star->sprite);
        free(star);
        entry = g_slist_next(entry);
    }

    g_slist_free(sky->stars);
    SDL_FreeSurface(sky->image);
    free(sky);
}

void sky_update(struct sky* sky, struct game* ctx, Uint32 dticks)
{
    GSList* entry = sky->stars;
    while (entry)
    {
        struct star* star = entry->data;
        struct sprite* spr = star->sprite;

        sprite_move(spr, 0, star->speed);
        if (spr->y > ctx->area.h)
            sprite_move(spr, 0, -ctx->area.h);

        sprite_blit(spr, ctx->screen, sky->image);
        entry = g_slist_next(entry);
    }
}

